public class Raboof { public static void main(String[] args) { System.out.print("What is your name?\n"); String name = StdIn.readLine(); int counter = 0; StdOut.print(name + ", think of a number between 1 and 50 inclusive, and I'll guess it.\n"); counter = counter + 1; int guess = 25; int max = 51; int min = 0; int guessedRight = 0; guess = (max + min)/2; StdOut.print("Is your number less than, greater than, or equal to 25?\n Type \"less\", \"greater\", or \"correct\".\n"); String answer = StdIn.readLine(); while (guessedRight != 1) { if (answer.equals("less")) { max = guess; guess = (max + min)/2; StdOut.print("Is your number less than, greater than, or equal to " + guess + "?\n Type \"less\", \"greater\", or \"correct\".\n"); counter = counter + 1; answer = StdIn.readLine(); } else if (answer.equals("greater")) { min = guess; guess = (max + min)/2; StdOut.print("Is your number less than, greater than, or equal to " + guess + "?\n Type \"less\", \"greater\", or \"correct\".\n"); counter = counter + 1; answer = StdIn.readLine(); } else if (answer.equals("correct")) { StdOut.print("Yay, I guessed it right in " + counter + " tries.\n"); guessedRight = 1; } else { StdOut.print("No, type \"less\", \"greater\", or \"correct\"!\n"); answer = StdIn.readLine(); } if (min >= max - 1) { StdOut.print(name + ", you are a liar. You are not following the instructions!\n"); guessedRight = 1; } } } }
